package com.oa.hyperion.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.oa.hyperion.entity.*;
import com.oa.hyperion.service.*;
import com.oa.hyperion.vo.DetailVo;
import com.oa.hyperion.vo.OutlineQuery;
import com.oa.hyperion.vo.TemplateUpdateVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Liu
 * @date 2022年01月17日 16:05
 */
@Service
public class CustomFormServiceImpl implements CustomFormService {
    private final ControllerService controllerService;
    private final ControllerTemplateService controllerTemplateService;
    private final DetailService detailService;
    private final OutlineService outlineService;
    private final TemplateService templateService;

    public CustomFormServiceImpl(ControllerService controllerService, ControllerTemplateService controllerTemplateService, DetailService detailService, OutlineService outlineService, TemplateService templateService) {
        this.controllerService = controllerService;
        this.controllerTemplateService = controllerTemplateService;
        this.detailService = detailService;
        this.outlineService = outlineService;
        this.templateService = templateService;
    }

    @Override
    public List<Template> getAllTemplate() {
        return templateService.findAllTemplates();
    }

    public Template getTemplate(int templateId) {
        return templateService.getById(templateId);

//        List<TemplateVo> templateVoList = new ArrayList<>();
//        List<Controller> controllerList = controllerService.list();
//        List<ControllerTemplateMerge> templateList = controllerTemplateService
//                .list(new QueryWrapper<ControllerTemplateMerge>().eq("template", templateId).orderByAsc("sorting"));
//
//        for (ControllerTemplateMerge merge : templateList) {
//            for (Controller controller : controllerList) {
//                if (controller.getId().equals(merge.getController())) {
//                    TemplateVo templateVo = new TemplateVo(controller, merge);
//                    templateVoList.add(templateVo);
//                    break;
//                }
//            }
//        }
//        return templateVoList;

    }

    @Override
    public List<Controller> getAllController() {
        return controllerService.list();
    }

    @Override
    public List<Outline> getAllOutlineByTemplate(int templateId) {
        return outlineService.list(new QueryWrapper<Outline>().eq("template",templateId));
    }

    @Override
    public void saveTemplate(Template template) {
        template.setStatus("y");
        templateService.save(template);
    }

    @Override
    public List<DetailVo> getOutlineDetail(int outlineId) {
        List<DetailVo> detailVos = new ArrayList<>();
        //获取所有控件
        List<Controller> controllerList = controllerService.list();
        //根据点击的概述id查找详情
        List<Detail> detailList = detailService.list(new QueryWrapper<Detail>().eq("outline", outlineId));
        List<Integer> ids = new ArrayList<>();
        //获取详情对应的模板控件ID
        for (Detail detail : detailList) {
            ids.add(detail.getControllerTemplate());
        }
        //获取模板控件
        List<ControllerTemplateMerge> controllerTemplateMerges = controllerTemplateService.list(new QueryWrapper<ControllerTemplateMerge>().in("id", ids).orderByAsc("sorting"));

        for (ControllerTemplateMerge controllerTemplateMerge : controllerTemplateMerges) {
            Detail d1 = null;
            Controller c1 = null;
            for (Detail detail : detailList) {
                if (controllerTemplateMerge.getId().equals(detail.getControllerTemplate())) {
                    d1 = detail;
                    break;
                }
            }
            for (Controller controller : controllerList) {
                if (controllerTemplateMerge.getController().equals(controller.getId())) {
                    c1 = controller;
                    break;
                }
            }
            assert c1 != null;
            assert d1 != null;
            DetailVo detailVo = new DetailVo(c1, d1, controllerTemplateMerge);
            detailVos.add(detailVo);

        }
        return detailVos;
    }

    @Override
    @Transactional
    public void saveDetail(int creator, List<Detail> details) {
        Outline outline = new Outline();
        outline.setApplicant(creator);
//        outline.setTitle();
    }

    @Override
    public void updateTemplate(int templateId, TemplateUpdateVo templateUpdateVo) {
        Template template = templateService.getById(templateId);
        if (templateUpdateVo.getName() != null) {
            template.setName(templateUpdateVo.getName());
        }
        if (templateUpdateVo.getStatus() != null) {
            template.setStatus(templateUpdateVo.getStatus());
        }
        templateService.updateById(template);
    }

    @Override
    public void deleteTemplate(Integer templateId) {
        templateService.deleteTemplate(templateId);
    }

    @Override
    public Outline getOutline(Integer id) {
        return outlineService.getById(id);
    }

    @Override
    public void saveOutline(Outline outline) {
        outlineService.save(outline);
    }

    @Override
    public Page<Outline> getOutlineByUId(Integer id, OutlineQuery outlineQuery) {
        System.out.println(outlineQuery);
        QueryWrapper<Outline> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("applicant", id);
        if (StringUtils.hasText(outlineQuery.getCheckStatus())) {
            queryWrapper.eq("check_status", outlineQuery.getCheckStatus());
        }
        if (StringUtils.hasText(outlineQuery.getFormStatus())) {
            queryWrapper.eq("status", outlineQuery.getFormStatus());
        }else {
            queryWrapper.ne("status", "n");
        }
        if (outlineQuery.getTemplate() != null)
            queryWrapper.eq("template_id", outlineQuery.getTemplate());
        return outlineService.page(new Page<>(outlineQuery.getPage(), outlineQuery.getLimit())
                , queryWrapper);
    }

    @Override
    public void deleteOutline(Integer id) {
        outlineService.deleteOutline(id);
    }

    @Override
    public void updateOutline(Integer id) {
        outlineService.updateOutline(id);
    }


}
